﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ActorManager : MonoBehaviour
{

    public BattleManager bm;
    public WeaponManager wm;
    public ActorController ac;
    public StateManager sm;
    // Start is called before the first frame update
    private void Awake()
    {
        ac = gameObject.GetComponent<ActorController>();
        GameObject sensor = transform.Find("sensor").gameObject;
        GameObject model = ac.model; ;                
        bm = Bind<BattleManager>(sensor);
        wm = Bind<WeaponManager>(model);
        sm = Bind<StateManager>(gameObject);        
        
    }

    private T Bind<T>(GameObject go)  where T : IActorManagerChild{
        T tmpInst = go.GetComponent<T>();
        if (tmpInst == null) {
            tmpInst = go.AddComponent<T>();
        }
        tmpInst.am = this;
        return tmpInst;
    }

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void TryDoDamage() {
        if (sm.isImmortal) { 
        }
        else if (sm.isDefense)
        {
            Block();
        }
        else {
            sm.AddHP(-5f);
            if (sm.HP <= 0)
            {
                Die();
            }
            else {
                Hit();
            }
        }
    }

    public void Block()
    {
        ac.IssueTrigger("block");
    }

    public void Hit() {
        ac.IssueTrigger("hit");
    }

    public void Die()
    {
        ac.IssueTrigger("die");
    }
}
